package com.lujianfei.waterpower.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.lujianfei.waterpower.ui.settings.SetttingActivity;

public class SharePreferenceUtils {

    final static String TAG = "SharePreferenceUtils";
    SharedPreferences defaultSharedPreferences;

    static SharePreferenceUtils sharePreferenceUtils = new SharePreferenceUtils();

    private SharePreferenceUtils() {
        defaultSharedPreferences = SystemUtils.getApplicationContext().getSharedPreferences("water_power_settings", Context.MODE_PRIVATE);
    }

    public static SharePreferenceUtils getInstance() {
        return sharePreferenceUtils;
    }

    public double getPowerUnit() {
        if (!defaultSharedPreferences.contains(SetttingActivity.SettingsFragment.key_power_unit)){
            return 1.2d;
        }
        return Double.longBitsToDouble(defaultSharedPreferences.getLong(SetttingActivity.SettingsFragment.key_power_unit,0));
    }

    public void setPowerUnit(double powerUnit) {
        try {
            SharedPreferences.Editor edit = defaultSharedPreferences.edit();
            edit.putLong(SetttingActivity.SettingsFragment.key_power_unit, Double.doubleToRawLongBits(powerUnit)).commit();
        } catch (Exception e) {
            Log.e(TAG, "" + e);
        }
    }

    public double getWaterUnit() {
        if (!defaultSharedPreferences.contains(SetttingActivity.SettingsFragment.key_water_unit)){
            return 8d;
        }
        return Double.longBitsToDouble(defaultSharedPreferences.getLong(SetttingActivity.SettingsFragment.key_water_unit, 0));
    }

    public void setWaterUnit(double waterUnit) {
        try {
            SharedPreferences.Editor edit = defaultSharedPreferences.edit();
            edit.putLong(SetttingActivity.SettingsFragment.key_water_unit, Double.doubleToRawLongBits(waterUnit)).commit();
        } catch (Exception e) {
            Log.e(TAG, "" + e);
        }
    }
}
